package com.example.ll_video.utils;







import android.annotation.SuppressLint;
import android.os.Build;

import androidx.annotation.RequiresApi;

import java.nio.charset.StandardCharsets;
import  java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

/**
 * Created by Administrator on 2017/11/10.
 */

public class ThreeDES {
    public static final String IV = "12345678";
    public static final String KEY ="AndroidandIOS";
    public static final String KEY1 ="878d61715efd99a08082504d052a9fdf";

    private static final long salt = 196332542;

    /**
     * DESCBC加密
     *
     * @param src
     *            数据源
     * @param ，长度必须是8的倍数
     * @return 返回加密后的数据
     * @throws
     */
    @SuppressLint("NewApi")
    public static String encryptDESCBC(final String src) throws Exception {

        // --生成key,同时制定是des还是DESede,两者的key长度要求不同
        final DESKeySpec desKeySpec = new DESKeySpec(KEY.getBytes(StandardCharsets.UTF_8));
        final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        final SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

        // --加密向量
        final IvParameterSpec iv = new IvParameterSpec(IV.getBytes(StandardCharsets.UTF_8));

        // --通过Chipher执行加密得到的是一个byte的数组,Cipher.getInstance("DES")就是采用ECB模式,cipher.init(Cipher.ENCRYPT_MODE,
        // secretKey)就可以了.
        final Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey,iv);
        final byte[] b = cipher.doFinal(src.getBytes(StandardCharsets.UTF_8));
        return  Base64.getEncoder().encodeToString(b);
    }

    /**
     * DESCBC解密
     *
     * @param src
     *            数据源
     * @param
     *            ，长度必须是8的倍数
     * @return 返回解密后的原始数据
     * @throws
     */
    @SuppressLint("NewApi")
    public static String decryptDESCBC(final String src) throws Exception {
        // --通过base64,将字符串转成byte数组
        final byte[] bytesrc = Base64.getDecoder().decode(src);

        // --解密的key
        final DESKeySpec desKeySpec = new DESKeySpec(KEY.getBytes(StandardCharsets.UTF_8));
        final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        final SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

        // --向量
        final IvParameterSpec iv = new IvParameterSpec(IV.getBytes(StandardCharsets.UTF_8));

        // --Chipher对象解密Cipher.getInstance("DES")就是采用ECB模式,cipher.init(Cipher.DECRYPT_MODE,
        // secretKey)就可以了.
        final Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey,iv);
        final byte[] retByte = cipher.doFinal(bytesrc);

        return new String(retByte);

    }
    public static void main(String args[]) throws Exception{
        String str = "15995" ;
        System.out.println( "----加密前-----：" + str );
        String encodeStr = encryptDESCBC(str);
        System.out.println( "----加密后-----：" + encodeStr );
        System.out.println( "----解密后-----：" + decryptDESCBC( encodeStr));


    }
}
